package com.tiancheng.trade.authserver.controller;

import com.tiancheng.trade.authserver.enums.ThirdPlatformEnums;
import com.tiancheng.trade.authserver.service.AuthLoginService;
import com.tiancheng.trade.authserver.service.IAuthUserService;
import com.tiancheng.trade.authserver.vo.*;
import com.tiancheng.trade.authserver.vo.response.WeChatLoginResponseVO;
import com.tiancheng.trade.commom.core.data.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 登陆相关接口controller
 */
@Slf4j
@RestController
@RequestMapping("/auth/loginIn")
public class AuthLoginInController {


    @Resource
    private AuthLoginService service;
    @Resource
    private IAuthUserService authUserService;

    /**
     * 用户登陆
     */
    @PostMapping("/userSignIn")
    public Result<UserLogInResultVO> userSignIn(@RequestBody UserLoginInVO param) {
        return Result.success(service.userSignIn(param, true));
    }

    /**
     * 应用程序登陆(使用相关信息换取access_token)
     */
    @PostMapping("/appAccessToken")
    public Result<ApplicationAccessTokenResponseVO> accessToken(@RequestBody @Validated ApplicationAccessTokenRequestVO param) {
        return Result.success(this.service.getAccessToken(param));
    }

    /**
     * 用户注册
     */
    @PostMapping("/signUp")
    public Result<Long> signUp(@RequestBody UserSignUpRequestVO param) {
        final Long useId = authUserService.signUp(param);
        return Result.success(useId);
    }

    /**
     * 微信公众号陆接口
     */
    @PostMapping({"/weChatSignIn"})
    public Result<UserLogInResultVO> weChatSignIn(@RequestBody WeChatLoginVO param) {
        UserLoginInVO loginParam=new UserLoginInVO();
        loginParam.setClientId(param.getAppId());
        loginParam.setOauth2Code(param.getCode());
        loginParam.setThirdPlatform(ThirdPlatformEnums.wechat_miniapp);
        return Result.success(service.userSignIn(loginParam, true));
    }

}
